package com.etoak.service.impl;

import cn.hutool.core.date.DateUtil;
import com.etoak.common.core.vo.PageVO;
import com.etoak.common.web.exception.CustomException;
import com.etoak.entity.Ingredients;
import com.etoak.mapper.IngredientsMapper;
import com.etoak.service.DishesService;
import com.etoak.service.IngredientsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author et2406
 * @since 2024-11-18
 */
@Service
public class IngredientsServiceImpl extends ServiceImpl<IngredientsMapper, Ingredients> implements IngredientsService {
    @Autowired
    DishesService dishesService;
    @Override
    public void add(Ingredients ingredients) {
        //校验食材名称
        if (ObjectUtils.isNotEmpty(getByName(ingredients.getName()))) {
            throw new CustomException("食材名称重复");
        }
        ingredients.setCreateTime(DateUtil.now());
        //插入数据库
        this.save(ingredients);
    }

    @Override
    public PageVO<Ingredients> getList(int pageNum, int pageSize, Ingredients ingredients) {

        PageHelper.startPage(pageNum, pageSize);

        List<Ingredients> list = baseMapper.getList(ingredients);

        PageInfo<Ingredients> pageInfo = new PageInfo<>(list);

        return new PageVO<>(pageInfo.getPageNum(), pageInfo.getPageSize(), list, pageInfo.getTotal());

    }

    @Override
    public void update(int id, Ingredients ingredients) {
        /*验证id*/
        if (ObjectUtils.isEmpty(this.getById(id))) {
            throw new CustomException("食材不存在");
        }
        /*不为空时判断*/
        String ingredientsName = ingredients.getName();
        if(StringUtils.isNotEmpty(ingredientsName)){
            Ingredients byName = this.getByName(ingredientsName);
            if (ObjectUtils.isNotEmpty(byName) && !byName.getId().equals(id)){
                throw new CustomException("食材重复");
            }
        }
        //更新
        ingredients.setId(id);
        ingredients.setUpdateTime(DateUtil.now());
        this.updateById(ingredients);


    }

    @Override
    public void delete(int id) {
        if(ObjectUtils.isEmpty(this.getById(id))){
            throw new CustomException("食材不存在");
        }
        //判断食材有没有被启用
        if(dishesService.inIngredientsUsed(id)){
            throw new CustomException("食材已经被使用中");
        }
        this.removeById(id);
    }

    @Override
    public List<Ingredients> listByType(String type) {
        Ingredients ingredients = new Ingredients();
        ingredients.setType(type);
        return baseMapper.getList(ingredients);

    }

    /**
     * 根据食材名称查询食材信息
     *
     * @param name
     * @return
     */
    private Ingredients getByName(String name) {
        return lambdaQuery().eq(Ingredients::getName, name).one();
    }
}
